<html>
<head><meta charset="utf-8"><title>get syntax tree with metadata · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/get.20syntax.20tree.20with.20metadata.html">get syntax tree with metadata</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="203882818"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/get%20syntax%20tree%20with%20metadata/near/203882818" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benedikt Mandelkow <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/get.20syntax.20tree.20with.20metadata.html#203882818">(Jul 14 2020 at 20:23)</a>:</h4>
<p>Hello I would like to write a tool which parses a rust crate and generates metadata about fn/structs etc.. This should include the places they are used and at the place of their use I'd need to figure out where they were defined. So I basically would like an iterator over all items in a file and get their source location/ comments/ type/ metadata if applicable or else get where they are used. I think using rust analyzer this should be fairly easy and would make things like "go to definition" on sourcehut possible <a href="https://drewdevault.com/2019/07/08/Announcing-annotations-for-sourcehut.html">https://drewdevault.com/2019/07/08/Announcing-annotations-for-sourcehut.html</a> . I tried the ra_ide crate and ra_syntax crate but could not quite find the right part of the API. As far as I can see my use case is a bit unusual for RA because its kind of greedy whereas RA is more lazy/ on demand so if its not really supported API that would be a helpful answer as well. Thanks!</p>



<a name="203900977"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/get%20syntax%20tree%20with%20metadata/near/203900977" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mikail Bagishov <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/get.20syntax.20tree.20with.20metadata.html#203900977">(Jul 14 2020 at 23:31)</a>:</h4>
<p>AFAIK you should look at ra_hir, it provides name resolution, type inference and so on.</p>



<a name="203943336"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/get%20syntax%20tree%20with%20metadata/near/203943336" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benedikt Mandelkow <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/get.20syntax.20tree.20with.20metadata.html#203943336">(Jul 15 2020 at 11:34)</a>:</h4>
<p>So what I think I can do is something along the lines of using ra_syntax to iterate over all tokens or some other nodes of the tree and for the ones that are interesting (like e.g. function, struct definitions and uses) invoke ra_ide to get the information for that position in the file but maybe there is an existing iterator-like thing I'm missing that would make this easier. <br>
For ha_hir I was not sure how to deal with the databases/ construct them/ there are a few of them. My approach was whenever I saw some struct I wanted to use, search where it was returned and this did not yield the information I wanted in these cases.</p>



<a name="203947588"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/get%20syntax%20tree%20with%20metadata/near/203947588" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/get.20syntax.20tree.20with.20metadata.html#203947588">(Jul 15 2020 at 12:20)</a>:</h4>
<p>Using ra_syntax and ra_ide in that way is probably the right approach</p>



<a name="203947772"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/get%20syntax%20tree%20with%20metadata/near/203947772" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/get.20syntax.20tree.20with.20metadata.html#203947772">(Jul 15 2020 at 12:22)</a>:</h4>
<p>you could also take a look at the syntax highlighting code (<code>syntax_highlighting.rs</code>) for an example of something similar</p>



<a name="203947923"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/get%20syntax%20tree%20with%20metadata/near/203947923" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/get.20syntax.20tree.20with.20metadata.html#203947923">(Jul 15 2020 at 12:24)</a>:</h4>
<p>and at <code>analysis_stats.rs</code> for an example of doing semantic analysis on a crate, but you're probably better off with the IDE level abstractions unless you want to do something special that they don't provide</p>



<a name="203948001"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/get%20syntax%20tree%20with%20metadata/near/203948001" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/get.20syntax.20tree.20with.20metadata.html#203948001">(Jul 15 2020 at 12:25)</a>:</h4>
<p>you get a database by calling <code>raw_database()</code> on an <code>AnalysisHost</code></p>



<a name="203965351"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/get%20syntax%20tree%20with%20metadata/near/203965351" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benedikt Mandelkow <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/get.20syntax.20tree.20with.20metadata.html#203965351">(Jul 15 2020 at 14:52)</a>:</h4>
<p>Ok thanks!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>